<?php

class MY_Controller extends CI_Controller {

    public function __construct() {
        parent::__construct();

        header("Content-type: text/html; charset=utf-8");
        ini_set('session.gc_maxlifetime', 288000);

        $this->img_host = "http://www.tan66.com";

        $this->load->model('users_model');
        $this->load->model('account_model');
        $this->load->model('bbs_user_model');
        $this->load->model('bbs_notice_model');
        $this->load->library('Crypt');
        $this->load->driver('cache'); //导入缓存驱动类
        $this->memcached = $this->cache->memcached; //全局memcached句柄
        //URL来路
        $HTTP_REFERER = @$_SERVER['HTTP_REFERER'];
        $this->redirectLink = $HTTP_REFERER ? $HTTP_REFERER : site_url();

        $this->remote_url = array(
            "sms_url" => "http://api.tan66.com/index.php?r=sms/send",
        );
        $request_plat = $this->session->userdata('request_plat');
        $request_flag = $this->session->userdata('request_flag');
        //来路属性
        $this->request_plat = @$_GET['plat'] <> '' ? @$_GET['plat'] : $request_plat;
        $this->request_flag = @$_GET['flag'] <> '' ? @$_GET['flag'] : $request_flag;


        //来源重定义
        if ($this->request_plat == "jx") {
            $this->request_plat = 1;
        }

        if ($this->request_plat<>2 && $this->request_plat <> 3){
            if (!($this->input->cookie("first_access"))){
                $this->input->set_cookie("first_access",1,3600*24*30);
                $first_access = 0;
            }else{
                $first_access = $this->input->cookie("first_access");
            }

        }else{
            $first_access = 1;
        }
        $this->assign('first_access', $first_access);

        $this->session->set_userdata('request_plat', $this->request_plat);
        $this->session->set_userdata('request_flag', $this->request_flag);

        //加载任务配置
        $this->settings->load('task');
        $this->config_task = setting('task');
        //加载奖励配置
        $this->settings->load('award');
        $this->config_award = setting('award');

        //判断登录状态
        $this->userinfo = $this->session->userdata('userinfo');
        if ($this->userinfo) {
            $this->user_id = decrypt_user_id($this->userinfo['uuid']);
            $user_info_ = $this->db->query("select i.nickname,i.head_img from tan_users_info i where i.user_id = " . $this->user_id . " order by i.user_id limit 1")->row_array();
            $this->userinfo['head_img'] = strstr($user_info_['head_img'], "http") ? $user_info_['head_img'] : $this->img_host . $user_info_['head_img'];
            $this->assign('uinfo', $this->userinfo);
        }
        $this->assign("alogin", $this->userinfo ? json_encode(array("uuid" => $this->userinfo['uuid'], "uname" => $this->userinfo['uname'], "isline" => 1)) : json_encode(array("uuid" => 0, "isline" => 0, "uname" => "")));
        $this->assign("style_dir", STYLE_DIR); //全局样式路径
        //当前页面
        $this->action = $this->uri->segment(2) ? $this->uri->segment(2) : 'index';
        $this->wxjsapi_config();
        //用户客户端信息
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        $this->assign("is_weixin", strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ? true : false);

        //网站配置相关
        $this->website_config = $this->db->query("select * from tan_website_config where id = 1 order by id desc limit 1")->row_array();
    }

    public function assign($key, $val) {
        $this->cismarty->assign($key, $val);
    }

    public function display($html) {
        $this->cismarty->display($html);
    }

    //添加系统消息
    public function insert_notice($data) {
        $this->bbs_notice_model->insert($data);
    }

    private function wxjsapi_config() {
        //获取缓存微信接口配置
        $wxjsapi_config = $this->memcached->get("wxjsapi_config_test");
        if (!$wxjsapi_config) {
            $wx_appid = "wxaaf92619fe7838bd";
            $wx_secret = "4f0c16a767af80e499b10c6c6b006931";
            $res = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $wx_appid . '&secret=' . $wx_secret);
            $res = json_decode($res, true);
            $token = $res['access_token'];
            $jsapi_ticket = file_get_contents('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $token . '&type=jsapi');
            $jsapi_ticket = json_decode($jsapi_ticket, true);
            $wxjsapi_config = array(
                "appid" => "wxaaf92619fe7838bd",
                "timestamp" => time(),
                "nonceStr" => str_replace(".", "", sprintf("%.3f", microtime(true))),
                "jsapi_ticket" => $jsapi_ticket['ticket'],
            );
            $wxjsapi_config = serialize($wxjsapi_config);
            $this->memcached->save("wxjsapi_config_test", $wxjsapi_config, 7200); //缓存2个小时
        }
        $curren_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $wxjsapi_config = unserialize($wxjsapi_config);
        $wxjsapi_config['signature'] = sha1("jsapi_ticket=$wxjsapi_config[jsapi_ticket]&noncestr=$wxjsapi_config[nonceStr]&timestamp=$wxjsapi_config[timestamp]&url=" . $curren_url);
        $this->assign("wxjsapi_config", $wxjsapi_config);
    }

    //设置时间进度1
    public function set_time($time) {
        $curent_time = time();
        $_second = $curent_time - $time;
        $_time = $_second / 60; //分钟
        if ($_time < 1) {
            return $_second . "秒前";
        }

        if ($_time > 1 && $_time < 60) {
            return intval($_time) . "分前";
        }

        if ($_time > 60 && $_time < 1440) {
            return intval($_time / 60) . "小时前";
        }

        if ($_time > 1440 && $_time < 7200) {
            return intval($_time / 1440) . "天前";
        }

        if ($_time > 7200) {
            return date("Y-m-d H:i", $time);
        }
    }

    //设置时间进度2
    public function set_time_two($time) {
        $curent_time = time();
        $_second = $curent_time - $time;
        $_time = $_second / 60; //分钟
        if ($_time < 1) {
            return $_second . "秒前";
        }

        if ($_time > 1 && $_time < 60) {
            return intval($_time) . "分前";
        }

        if ($_time > 60 && $_time < 1440) {
            return intval($_time / 60) . "小时前";
        }

        if ($_time > 1440) {
            return date("Y-m-d H:i", $time);
        }
    }

    public function set_time_three($time) {
        $curent_time = time();
        $_second = $curent_time - $time;
        $_time = $_second / 60; //分钟
        if ($_time < 1) {
            return $_second . "秒前";
        }

        if ($_time > 1 && $_time < 60) {
            return intval($_time) . "分前";
        }

        if ($_time > 60 && $_time < 1440) {
            return intval($_time / 60) . "小时前";
        }

        if ($_time > 1440) {
            return intval($_time / 1440) . "天前";
        }
    }

    //溜溜币奖励类型
    public function finance_award_type($type) {
        $type_ = "";
        switch ($type) {
            case "reg_award":
                $type_ = "注册奖励";
                break;
            case "task_award":
                $type_ = "任务奖励";
                break;
            case "exchange_award":
                $type_ = "兑换奖励";
                break;
            case "sign_award":
                $type_ = "签到奖励";
                break;
            case "withdraw_award":
                $type_ = "提现奖励";
                break;
            case "spread_award":
                $type_ = "推广奖励";
                break;
            case "exper_award":
                $type_ = "体验奖励";
                break;
            case "other_award":
                $type_ = "其他奖励";
                break;
            case "withdraw_deduct":
                $type_ = "提现扣除";
                break;
            case "exchange_deduct":
                $type_ = "兑换扣除";
                break;
            case "interest_award":
                $type_ = "加息奖励";
                break;
        }
        return $type_;
    }

    function get_next_request_url() {
        $url = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
        return "?redirectUrl=" . urlencode($url);
    }

    function add_reload_log($data) {

        return $this->db->insert("reload_log", $data);
    }

    function in_reload_log($type = 1, $type_val = 0, $jump_url = '') {
        $data['type'] = $type;
        $data['type_val'] = $type_val;
        $data['jump_url'] = $jump_url;
        $data['request_ip'] = $this->input->ip_address();
        $data['request_from'] = $_SERVER['HTTP_REFERER'];
        $data['request_plat'] = $this->request_plat ? $this->request_plat : 0;
        $data['request_flag'] = $this->request_flag ? $this->request_flag : 0;
        $data['request_time'] = time();

        $this->add_reload_log($data);
    }

}
